/**
 * 
 * @Author TOUR U6 WS 2013 PM&T 
 * @Created 03.12.2013
 * 
 */
package de.hhn.pmt.tour.u6.gui.organisator;

import java.awt.EventQueue;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;

import org.orm.PersistentException;

import de.hhn.pmt.tour.u6.Tour;
import de.hhn.pmt.tour.u6.TourDAO;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

/**
 * The Class Tour_GUI.
 * 
 * Alexander Bader
 */
public class Tour_GUI extends JFrame {
	
	/** The content pane. */
	private JPanel contentPane;
	
	/** The text field_1. */
	private JTextField textField_1;
	
	/** The combo box_1. */
	private JComboBox comboBox_1;
	
	/** The text area. */
	private JTextArea textArea;
	
	/** The text area_1. */
	private JTextArea textArea_1;
	
	/** The lbl hinweis. */
	private JLabel lblHinweis;
	
	/** The alle touren. */
	private Tour[] alleTouren;

	/**
	 * Launch the application.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Tour_GUI frame = new Tour_GUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Tour_GUI() {
		setTitle("Tour erstellen & Informationen hinzuf\u00FCgen");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 425, 496);
		contentPane = new JPanel();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblTourHinzufgen = new JLabel("Tour:");
		lblTourHinzufgen.setBounds(15, 246, 31, 17);
		lblTourHinzufgen.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JLabel lblTour = new JLabel("Bezeichnung:");
		lblTour.setBounds(15, 31, 74, 17);
		lblTour.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		textField_1 = new JTextField();
		textField_1.setBounds(93, 28, 298, 20);
		textField_1.setColumns(10);
		
		JLabel lblNeueTour = new JLabel("Neue Tour erstellen:");
		lblNeueTour.setBounds(15, 5, 131, 17);
		lblNeueTour.setFont(new Font("Tahoma", Font.BOLD, 13));
		
		JButton btnHinzufgen = new JButton("Speichern");
		btnHinzufgen.setBounds(15, 411, 141, 25);
		btnHinzufgen.setFont(new Font("Tahoma", Font.BOLD, 12));
		
		btnHinzufgen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Tour selectedTour = (Tour) comboBox_1.getSelectedItem();
				selectedTour.setInformation(textArea.getText());
				
				try {
					TourDAO.save(selectedTour);
					lblHinweis.setText("Die Tour \"" + ((Tour) comboBox_1.getSelectedItem()).getBezeichnung() + "\" wurde erfolgreich bearbeitet!");
					lblHinweis.setForeground(Color.BLUE);
				} catch (PersistentException e) {
					lblHinweis.setText("Die Tour \"" + ((Tour) comboBox_1.getSelectedItem()).getBezeichnung() + "\" konnte nicht bearbeitet werden!");
					lblHinweis.setForeground(Color.RED);
					e.printStackTrace();
				}
			}
		});

		JButton btnErstellen = new JButton("Erstellen");
		btnErstellen.setBounds(15, 156, 124, 25);
		btnErstellen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Tour createdTour = TourDAO.createTour();
				createdTour.setBezeichnung(textField_1.getText());
				createdTour.setInformation(textArea_1.getText());
				for(Tour t : alleTouren){
					if(t.getBezeichnung().equals(textField_1.getText())) {
						lblHinweis.setText("Die Tour \"" + textField_1.getText() + "\" konnte nicht erstellt werden!");
						lblHinweis.setForeground(Color.RED);
						return;
					}
				}
				try {
					TourDAO.save(createdTour);
					lblHinweis.setText("Die Tour \"" + textField_1.getText() + "\" wurde erfolgreich erstellt!");
					lblHinweis.setForeground(Color.BLUE);
				} catch (PersistentException e) {
					lblHinweis.setText("Die Tour \"" + textField_1.getText() + "\" konnte nicht erstellt werden!");
					lblHinweis.setForeground(Color.RED);
					e.printStackTrace();
				}
			}
		});
		btnErstellen.setFont(new Font("Tahoma", Font.BOLD, 12));
		
		comboBox_1 = new JComboBox();
		comboBox_1.setBounds(64, 245, 106, 20);
		
		JLabel lblInformationen = new JLabel("Informationen:");
		lblInformationen.setBounds(15, 271, 83, 17);
		lblInformationen.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		JLabel lblInformationenZuEiner = new JLabel("Informationen einer Tour bearbeiten:");
		lblInformationenZuEiner.setBounds(15, 222, 233, 17);
		lblInformationenZuEiner.setFont(new Font("Tahoma", Font.BOLD, 12));
		
		JLabel lblInformationen_1 = new JLabel("Informationen:");
		lblInformationen_1.setBounds(15, 54, 83, 17);
		lblInformationen_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		
		lblHinweis = new JLabel(" ");
		lblHinweis.setBounds(15, 192, 369, 14);
		
		JButton btnErneuern = new JButton("Erneuern");
		btnErneuern.setBounds(253, 411, 141, 25);
		btnErneuern.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				comboBox_1.removeAllItems();
				fill();
			}
		});
		btnErneuern.setFont(new Font("Tahoma", Font.BOLD, 12));
		contentPane.setLayout(null);
		contentPane.add(lblNeueTour);
		contentPane.add(btnErstellen);
		contentPane.add(lblInformationen_1);
		contentPane.add(lblInformationenZuEiner);
		contentPane.add(lblTourHinzufgen);
		contentPane.add(comboBox_1);
		contentPane.add(lblInformationen);
		contentPane.add(btnHinzufgen);
		contentPane.add(btnErneuern);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane_1.setBounds(15, 294, 376, 106);
		contentPane.add(scrollPane_1);
		
		textArea = new JTextArea();
		scrollPane_1.setViewportView(textArea);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(15, 77, 376, 73);
		contentPane.add(scrollPane);
		
		textArea_1 = new JTextArea();
		scrollPane.setViewportView(textArea_1);
		textArea_1.setLineWrap(true);
		textArea_1.setWrapStyleWord(true);
		contentPane.add(lblTour);
		contentPane.add(textField_1);
		contentPane.add(lblHinweis);
		fill();
	}
	
	/**
	 * Fill.
	 */
	private void fill() {
		try {
			alleTouren = TourDAO.listTourByQuery(null, "bezeichnung");
		} catch (PersistentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(Tour t : alleTouren) {
			comboBox_1.addItem(t);
		}
		
	}

	/**
	 * Gets the text area_1.
	 *
	 * @return the text area_1
	 */
	public JTextArea getTextArea_1() {
		return textArea;
	}
	
	/**
	 * Gets the text field_1.
	 *
	 * @return the text field_1
	 */
	public JTextField getTextField_1() {
		return textField_1;
	}
	
	/**
	 * Gets the text area_2.
	 *
	 * @return the text area_2
	 */
	public JTextArea getTextArea_2() {
		return textArea_1;
	}
	
	/**
	 * Gets the label.
	 *
	 * @return the label
	 */
	public JLabel getLabel() {
		return lblHinweis;
	}
}
